#ifndef TEXTINPUT_H
#define TEXTINPUT_H

#include "aerial\Object\Object.h"

#include "hge\hgefont.h"
#include "hge\hgesprite.h"
#include "hge\hgerect.h"
#include "hge\hgeresource.h"

namespace air
{
	class TextInput : public IGuiObject
	{
	public:
		TextInput(hgeResourceManager* hgeResMan, float _x, float _y, float _width, float _height, DWORD _color = 0xFF000000, int _capacity = 100, const char *_text = "");
		TextInput(hgeResourceManager* hgeResMan, float _width, float _height, DWORD _color = 0xFF000000, int _capacity = 100, const char *_text = "");
		~TextInput();

		bool TextInput::KeyClick(int key, int chr);
		void SetTextColor(DWORD _color)	{ color.SetHWColor(_color); }
		void SetBorderColor(DWORD _color) { color.SetHWColor(_color); }
		int  GetSize() const { return size; }
		const char *GetText() const  { return text; }
		/// @brief Copy, test for multiline and get size.
		/// Cursor pos was not changed
		/// @return Whether successful. Text will be modified even false.
		bool SetText(const char *_text);

		bool GetMultiLine() const  { return isMultiLine; }
		/// @brief If is multiline, press Enter will start a new line.
		/// Else it will call a function you specificated or do nothing
		void SetMultiLine(bool _isMultiLine)
		{
			isMultiLine = _isMultiLine;
			updateCursorPos();
		}
		void SetMaxSize(int _maxSize)
		{
			if (_maxSize < size)
				maxSize = size;
			maxSize = _maxSize;
		}
		int GetMaxSize() const { return maxSize; }

		void Focus(bool bFocused)
		{
			if (bFocused) 
				borderColor = borderColorFocused;
			else borderColor = borderColorNorm;
		}

		void MouseOver(bool bOver)
		{
			if (bOver) borderColor = borderColorOver;
			else borderColor = borderColorNorm;
		}

		/// @brief text color
		hgeColor	color;
		hgeColor	borderColorNorm;
		hgeColor	borderColorOver;
		hgeColor	borderColorFocused;
		/// @brief distance between the text and the border
		float		padTop;
		float		padLeft;
		/// @brief for tuning cursor position
		float		cursorXOffset;
		float		cursorYOffset;
		/// @brief must be greater than zero
		float		blinkInterval;
		/// @brief whether TAB key insert tab, default false
		bool        tabInsert;

		/// @brief the function to call when Enter is pressed in single
		/// line mode
		void (*enterHook)();
		
		void Render();

	private:
		TextInput(const TextInput &rhs);
		TextInput& operator=(const TextInput &rhs);

		bool expand();
		void updateCursorPos();

		bool insertChr(int chr);
		bool backspace();
		bool backwardChar();
		bool forwardChar();
		bool deleteChar();

		hgeColor	borderColor;
		
		int		pos;
		bool	focused;
		float	cursorX;
		float	cursorY;
		bool	isMultiLine;

		char	*text;
		int		size;
		int		capacity;
		int		maxSize;

		float	blinkCounter;

		hgeFont		*fnt;
	};
}

#endif /* TEXTINPUT_H */
